<template>
  <quill-editor :id="randomId(3)"
                v-model="content"
                :ref="editorRef"
                :options="editorOption"
                @focus="onEditorFocus($event)"
                @blur="onEditorBlur($event)"
                @change="onEditorChange($event)"
                class="editor"
                :disabled="quillDisabled"
                @ready="onEditorReady($event)" />
</template>
<script>
import { Quill } from 'vue-quill-editor'
import { lineHeightStyle } from '../../public/static/js/lineHeight'
// import ImageResize from 'quill-image-resize-module'
// Quill.register('modules/imageResize', ImageResize)
// import { ImageDrop } from 'quill-image-drop-module'
// Quill.register('modules/imageDrop', ImageDrop)
let fontSizeStyle = Quill.import('attributors/style/size')
let Align = Quill.import('attributors/style/align') // 引入这个后会把对齐类型样式写在style上
Align.whitelist = ['right', 'center', 'justify']
Quill.register(Align, true)
let Font = Quill.import('attributors/style/font') // 引入这个后会把对齐类型样式写在style上
let fonts = [
  false,
  'SimSun',
  'SimHei',
  'Microsoft-YaHei',
  'KaiTi',
  'FangSong',
  'Arial',
  'Times-New-Roman',
  'sans-serif',
  '宋体',
  '黑体'
]
Font.whitelist = fonts
Quill.register(Font, true)
fontSizeStyle.whitelist = [
  '12px',
  '14px',
  '16px',
  '18px',
  '20px',
  '22px',
  '24px',
  '26px',
  '28px',
  '30px',
  '32px',
  '36px',
  '38px',
  '40px',
  '45px',
  '50px',
  false
]
Quill.register(fontSizeStyle, true)
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], //加粗，斜体，下划线，删除线
  ['blockquote', 'code-block'], //引用，代码块
  [{ size: fontSizeStyle.whitelist }],
  // [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
  [{ list: 'ordered' }, { list: 'bullet' }], //列表
  [{ script: 'sub' }, { script: 'super' }], // 上下标
  [{ indent: '-1' }, { indent: '+1' }], // 缩进
  // [{ direction: 'rtl' }], // 文本方向
  // [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
  [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
  [{ font: Font.whitelist }], //字体
  [{ align: [] }], //对齐方式
  [{ lineHeight: ['initial', '1', '1.5', '1.75', '2', '3', '4', '5'] }], //行高
  ['clean'], //清除字体样式
  ['image', 'video'] //上传图片、上传视频
]
const toolbarTips = [
  { Choice: '.ql-bold', title: '加粗' },
  { Choice: '.ql-italic', title: '倾斜' },
  { Choice: '.ql-underline', title: '下划线' },
  // { Choice: '.ql-header', title: '段落格式' },
  { Choice: '.ql-strike', title: '删除线' },
  // { Choice: '.ql-blockquote', title: '块引用' },
  { Choice: '.ql-code-block', title: '插入代码段' },
  // { Choice: '.ql-size', title: '字体大小' },
  { Choice: '.ql-list[value="ordered"]', title: '编号列表' },
  { Choice: '.ql-list[value="bullet"]', title: '项目列表' },
  // { Choice: '.ql-header[value="1"]', title: 'h1' },
  // { Choice: '.ql-header[value="2"]', title: 'h2' },
  { Choice: '.ql-script[value="sub"]', title: '下角标' },
  { Choice: '.ql-script[value="super"]', title: '上角标' },
  { Choice: '.ql-lineHeight', title: '行高' },
  { Choice: '.ql-font', title: '字体' },
  { Choice: '.ql-align', title: '对齐方式' },
  { Choice: '.ql-color', title: '字体颜色' },
  { Choice: '.ql-background', title: '背景颜色' },
  { Choice: '.ql-image', title: '图像' },
  { Choice: '.ql-video', title: '视频' },
  { Choice: '.ql-link', title: '添加链接' },
  { Choice: '.ql-formula', title: '插入公式' },
  { Choice: '.ql-clean', title: '清除格式' },
  { Choice: '.ql-indent[value="-1"]', title: '向左缩进' },
  { Choice: '.ql-indent[value="+1"]', title: '向右缩进' },
  // { Choice: '.ql-header .ql-picker-label', title: '标题大小' },
  // { Choice: '.ql-header .ql-picker-item[data-value="1"]', title: '标题一' },
  // { Choice: '.ql-header .ql-picker-item[data-value="2"]', title: '标题二' },
  // { Choice: '.ql-header .ql-picker-item[data-value="3"]', title: '标题三' },
  // { Choice: '.ql-header .ql-picker-item[data-value="4"]', title: '标题四' },
  // { Choice: '.ql-header .ql-picker-item[data-value="5"]', title: '标题五' },
  // { Choice: '.ql-header .ql-picker-item[data-value="6"]', title: '标题六' },
  // { Choice: '.ql-header .ql-picker-item:last-child', title: '标准' },
  // { Choice: '.ql-size .ql-picker-item[data-value="small"]', title: '小号' },
  // { Choice: '.ql-size .ql-picker-item[data-value="large"]', title: '大号' },
  // { Choice: '.ql-size .ql-picker-item[data-value="huge"]', title: '超大号' },
  { Choice: '.ql-size .ql-picker-item:nth-child(2)', title: '标准' },
  { Choice: '.ql-align .ql-picker-item:first-child', title: '居左对齐' },
  {
    Choice: '.ql-align .ql-picker-item[data-value="center"]',
    title: '居中对齐'
  },
  {
    Choice: '.ql-align .ql-picker-item[data-value="right"]',
    title: '居右对齐'
  },
  {
    Choice: '.ql-align .ql-picker-item[data-value="justify"]',
    title: '两端对齐'
  }
]
export default {
  props: {
    // 编辑器的内容
    editorContent: {
      type: String,
      required: true
    },
    editorRef: {
      type: String,
      required: true
    },
    quillDisabled: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      content: this.editorContent,
      editorOption: {
        theme: 'snow',
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
              lineHeight: value => {
                if (value) {
                  let quill = this.$refs.editorRef.quill
                  quill.format('lineHeight', value)
                }
              }
            }
          },
          // imageResize: {
          //   displayStyles: {
          //     backgroundColor: 'black',
          //     border: 'none',
          //     color: 'white'
          //   },
          //   modules: ['Resize', 'DisplaySize']
          //   // modules: ['Resize', 'DisplaySize', 'Toolbar']
          // },
          // imageDrop: true
        }
      }
    }
  },
  computed: {
    //当前富文本实例
    editor () {
      return this.$refs.editorRef.quillEditor
    }
  },
  watch: {
    editorContent () {
      this.content = this.editorContent
    }
  },
  mounted () {
    for (let item of toolbarTips) {
      let tip = document.querySelector('.editor ' + item.Choice)
      if (!tip) continue
      tip.setAttribute('title', item.title)
    }
  },
  methods: {
    randomId (len) {
      var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      var tempLen = chars.length,
        tempStr = ''
      for (var i = 0; i < len; ++i) {
        tempStr += chars.charAt(Math.floor(Math.random() * tempLen))
      }
      return tempStr
    },
    // 准备富文本编辑器
    onEditorReady (quill) {
      Quill.register({ 'formats/lineHeight': lineHeightStyle }, true)
    },
    // 富文本编辑器 失去焦点事件
    onEditorBlur ({ html }) {
      //内容改变事件
    },
    // 富文本编辑器 获得焦点事件
    onEditorFocus ({ html }) {
      this.$emit('clear', this.content)
    },
    // 富文本编辑器 内容改变事件
    onEditorChange ({ html }) {
      //内容改变事件
      // console.log('内容改变事件');
      this.$emit('input', this.content)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
